package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualityPhysicalDefectBo;
import com.ruoyi.quality.domain.bo.QualityPhysicalReportBo;
import com.ruoyi.quality.domain.vo.QualityPhysicalDefectVo;
import com.ruoyi.quality.domain.vo.QualityPhysicalReportVo;
import com.ruoyi.quality.service.IQualityPhysicalDefectService;
import com.ruoyi.quality.service.IQualityPhysicalReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 质量通报
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/physicalReport")
public class QualityPhysicalReportController extends BaseController {

    private final IQualityPhysicalReportService iQualityPhysicalReportService;
    private final IQualityPhysicalDefectService iQualityPhysicalDefectService;

    /**
     * 查询质量通报列表
     */
    @SaCheckPermission("quality:physicalReport:list")
    @GetMapping("/list")
    public TableDataInfo<QualityPhysicalReportVo> list(QualityPhysicalReportBo bo, PageQuery pageQuery) {
        return iQualityPhysicalReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询最新质量通报号
     */
    @SaCheckPermission("quality:physicalReport:list")
    @GetMapping("/getMaxNumber")
    public Long getMaxNumber(QualityPhysicalReportBo bo) {
        return iQualityPhysicalReportService.getMaxNumber(bo);
    }

    /**
     * 导出质量通报列表
     */
    @SaCheckPermission("quality:physicalReport:export")
    @Log(title = "质量通报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QualityPhysicalReportBo bo, HttpServletResponse response) {
        List<QualityPhysicalReportVo> list = iQualityPhysicalReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "质量通报", QualityPhysicalReportVo.class, response);
    }

    /**
     * 导出质量通报考核列表
     */
    @SaCheckPermission("quality:physicalReport:export")
    @Log(title = "质量通报考核表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportFine/{reportIds}")
    public void exportFine(@PathVariable Long[] reportIds, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        List<QualityPhysicalDefectVo> defectVoList = iQualityPhysicalDefectService.queryByReportIds(Arrays.asList(reportIds));
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, defectVoList), "单列表.xlsx", "excel/考核汇总表.xlsx", response);
    }

    /**
     * 导出质量通报闭环列表
     */
    @SaCheckPermission("quality:physicalReport:export")
    @Log(title = "质量通报闭环表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCycle/{reportIds}")
    public void exportCycle(@PathVariable Long[] reportIds, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        List<QualityPhysicalDefectVo> defectVoList = iQualityPhysicalDefectService.queryByReportIds(Arrays.asList(reportIds));
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, defectVoList), "单列表.xlsx", "excel/闭环汇总表.xlsx", response);
    }

    /**
     * 获取质量通报详细信息
     *
     * @param reportId 主键
     */
    @SaCheckPermission("quality:physicalReport:query")
    @GetMapping("/{reportId}")
    public R<QualityPhysicalReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long reportId) {
        QualityPhysicalDefectBo defectBo = new QualityPhysicalDefectBo();
        defectBo.setReportId(reportId);
        List<QualityPhysicalDefectVo> qualityPhysicalDefectVoList = iQualityPhysicalDefectService.queryList(defectBo);
        QualityPhysicalReportVo reportVo = iQualityPhysicalReportService.queryById(reportId);
        reportVo.setDefectList(qualityPhysicalDefectVoList);
        return R.ok(reportVo);
    }

    /**
     * 新增质量通报
     */
    @SaCheckPermission("quality:physicalReport:add")
    @Log(title = "质量通报", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityPhysicalReportBo bo) {
        List< QualityPhysicalDefectBo> qualityPhysicalDefectList = bo.getDefectList();
        Long reportId = iQualityPhysicalReportService.insertByBo(bo,true);
        Date reportDate = bo.getReportDate();
        return toAjax(iQualityPhysicalDefectService.insertOrUpdateBatch(qualityPhysicalDefectList,reportId,reportDate));
    }

    /**
     * 修改质量通报
     */
    @SaCheckPermission("quality:physicalReport:edit")
    @Log(title = "质量通报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityPhysicalReportBo bo) {
        List< QualityPhysicalDefectBo> qualityPhysicalDefectList = bo.getDefectList();
        Long reportId = bo.getReportId();
        Date reportDate = bo.getReportDate();
        List<Long> reportIds = new ArrayList<>();
        reportIds.add(reportId);
        iQualityPhysicalDefectService.deleteWithValidByReportIds(reportIds, false);
        iQualityPhysicalDefectService.insertOrUpdateBatch(qualityPhysicalDefectList,reportId,reportDate);
        return toAjax(iQualityPhysicalReportService.updateByBo(bo));
    }

    /**
     * 删除质量通报
     *
     * @param reportIds 主键串
     */
    @SaCheckPermission("quality:physicalReport:remove")
    @Log(title = "质量通报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] reportIds) {
        List<Long> ids = Arrays.asList(reportIds);
        iQualityPhysicalDefectService.deleteWithValidByReportIds(ids, false);
        return toAjax(iQualityPhysicalReportService.deleteWithValidByIds(Arrays.asList(reportIds), true));
    }
}
